package org.example.result;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * @description: 返回result工具类
 * @author: user
 * @create: 2022-08-21 15:17
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {

    private int code;
    private String msg;
    private T data;
    private Integer totalCount;

    private ServerResponse(int code) {
        this.code = code;
    }

    private ServerResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }

    private ServerResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    private ServerResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ServerResponse(int code,String msg,Integer totalCount,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.totalCount=totalCount;
    }

    public int getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    /**
     * 默认操作成功返回默认code
     *
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateSuccess() {
        return new ServerResponse<T>(ResponseCode.SERVICE_SUCCESS.getCode());
    }

    /**
     * 默认操作成功返回默认code与msg
     *
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateSuccessMsg() {
        return new ServerResponse<T>(ResponseCode.SERVICE_SUCCESS.getCode(), ResponseCode.SERVICE_SUCCESS.getDesc());
    }

    /**
     * 返回code与自定义操作成功msg
     *
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateSuccessMessage(String msg) {
        return new ServerResponse<T>(ResponseCode.SERVICE_SUCCESS.getCode(), msg);
    }

    /**
     * 操作成功返回code与data
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateSuccess(T data) {
        return new ServerResponse<T>(ResponseCode.SERVICE_SUCCESS.getCode(), ResponseCode.SERVICE_SUCCESS.getDesc(), data);
    }

    /**
     * 操作失败返回code，msg
     *
     * @param msg
     * @return
     */
    public static <T> ServerResponse<T> operateFail(String msg) {
        return new ServerResponse<T>(ResponseCode.SEEVICE_FAIL.getCode(), msg);
    }

    /**
     * 操作成功返回code、msg与data
     *
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateSuccess(String msg, T data) {
        return new ServerResponse<T>(ResponseCode.SERVICE_SUCCESS.getCode(), msg, data);
    }

    /**
     * 后台出错返回code、msg
     *
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateError() {
        return new ServerResponse<T>(ResponseCode.SERVICE_ERROR.getCode(), ResponseCode.SERVICE_ERROR.getDesc());
    }

    /**
     * 操作错误返回code、错误msg
     *
     * @param errorMessage
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.SERVICE_ERROR.getCode(), errorMessage);
    }

    /**
     * 自定义返回错误code 与msg
     *
     * @param errorCode
     * @param errorMessage
     * @param <T>
     * @return
     */
    public static <T> ServerResponse<T> operateErrorCodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

   /**
    * @description:获取分页数据信息（layui）
    * @author: user
    * @date: 2019-02-26 15:14:56
    * @param totalCount
    * @param data
    * @return com.spring.common.result.ServerResponse<T>
    */
    public static <T> ServerResponse<T> getPageAndData(Integer totalCount,T data){
        return new ServerResponse<T>(ResponseCode.SERVICE_SUCCESS.getCode(),ResponseCode.SERVICE_SUCCESS.getDesc(),totalCount,data);
    }

}
